package com.gams.device.readface;

import ReadSense.api.enumerate.AuthType;
import ReadSense.api.output.ReadSenseFace;
import ReadSense.api.structure.*;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class ReadFaceUtil
{
    public static void main(String[] args) throws IOException
    {
        String path = "D:/deploy/upload/2021/12/25/1640397047141.png";
        verify(path);
    }


    public static float verify(String path1) throws IOException
    {
        String path2 = "C:\\Program Files\\Java\\jdk1.8.0_231\\bin\\x64\\zp.jpg";
        return verify(path1, path2);
    }

    public static float verify(String path1, String path2) throws IOException
    {

        String licContentPath = null;
        String licAuthContentPath = null;

        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win"))
        {
            licContentPath = "license/afb7918d_windows_offline_license_content.lic";
            licAuthContentPath = "license/afb7918d_windows_offline_license_content.lic_auth";
        }


        RSLicInitParams params = new RSLicInitParams(licContentPath, licAuthContentPath, AuthType.OFFLINE);
        ReadSenseFace sdk = new ReadSenseFace(params);
        RSFaceRecognitionFeature rsFaceRecognitionFeature1 = ReadFaceSampleSingTest(sdk, path1);
        RSFaceRecognitionFeature rsFaceRecognitionFeature2 = ReadFaceSampleSingTest(sdk, path2);

        float similarity = sdk.CompareFeature(rsFaceRecognitionFeature1, rsFaceRecognitionFeature2);
        sdk.UnInit();
        return similarity;
    }


    // Track(追踪)
    // Attribute(属性)
    // IRLiveness(红外属性)。
    // 质量Quliaty可以用来过滤质量低的人脸(阈值0.32左右)，
    // RGBLiveness是可见光活体，是否需要由你们决定。
    public static RSFaceRecognitionFeature ReadFaceSampleSingTest(ReadSenseFace sdk, String sample) throws IOException
    {
        byte[] imageBuff = FileUtils.readFileToByteArray(new File(sample));

        List<RSFaceInfo> outDetect = sdk.Detect(imageBuff);
        System.out.println("Detect = :" + outDetect.toString());

        List<RSFaceInfo> outTrack = sdk.Track(imageBuff);
        System.out.println("Track = :" + outTrack.toString());

        RSFaceQualityScore faceQualityScore = sdk.GetQuality(imageBuff, outDetect.get(0));
        System.out.println("Quality = :" + faceQualityScore);

        RSFaceAttrResult faceAttrResult = sdk.GetAttribute(imageBuff, outDetect.get(0));
        System.out.println("Attribute = :" + faceAttrResult);

        RSFaceLivenessResult faceIRLivenessResult = sdk.InfraredLiveness(imageBuff, outDetect.get(0));
        System.out.println("IRLiveness = :" + faceIRLivenessResult);

        RSFaceLivenessResult faceRGBLivenessResult = sdk.RGBLiveness(imageBuff, outDetect.get(0));
        System.out.println("RGBLiveness = :" + faceRGBLivenessResult);

        RSFaceRecognitionFeature faceRecognitionFeature = sdk.GetFeature(imageBuff, outDetect.get(0));
        System.out.println("FaceFeature = :" + faceRecognitionFeature);
        return faceRecognitionFeature;
    }

}



